#include "g_local.h"
#include "ui_local.h"

GameImport *gi;
GameRenderer *r;

/* Startup linkage-related things */
bool CVar::linkedToEngine = false;
CVar *CVar::firstLink = NULL;

namespace og {
	namespace User
	{
		void Error( ErrorId id, const char *msg, const char *param ) {
			// Todo: Throw an exception on the id's you think are important.
			gi->UserError(id, msg, param);
		}
	}

	og::FileSystem *FS = NULL;
	og::CmdSystem *cmdSystem = NULL;
	og::CVarSystem *cvarSystem = NULL;
}

/*
===========================
GameClass::OnLoad() & OnUnload
This stuff all is called by the internal plugin system,
don't worry about it too much.
===========================
*/
void GameClass::OnLoad ( PluginImportBase *import )
{
	gi = (GameImport *)import;
	cvarSystem = gi->cvar;
	cmdSystem = gi->cmd;
	og::FS = gi->fs;
	r = gi->r;

	CommonSetFileSystem(og::FS);
}

void GameClass::OnUnload( void )
{
	gi = NULL;
	cvarSystem = NULL;
	cmdSystem = NULL;
	r = NULL;
}


/*
===========================
GameClass::GameInit()
Initializes the game logic DLL
===========================
*/

void GameClass::GameInit()
{
	gi->Print("-- GameClass::GameInit() --\n");

	CVar::LinkCVars();

	UI_Main();
}

/*
===========================
GameClass::GameShutdown()
Shuts down the game logic DLL
===========================
*/

void GameClass::GameShutdown()
{
	gi->Print("-- GameClass::GameShutdown() --\n");

	CVar::UnlinkCVars();
}

/*
===========================
GameClass::RunFrame()
Perform per-frame logic
===========================
*/

void GameClass::RunFrame()
{
	r->irrDriver->beginScene(true, true, irr::video::SColor(255,100,101,140));
	r->irrScene->drawAll();
	UI_Frame();

	r->irrGUI->drawAll();

	r->irrDriver->endScene();
}

/*
===========================
getPluginExport
Exported function which is called by VM_Create() in order
to get the exports which are called by the engine
===========================
*/

GameClass ge;

// This function gets exported, to be used by BaseEngine
PluginExportBase *getPluginExport(void)
{
	return &ge;
}